//
//  MindfulClockViewController.m
//  Myrna-Mindfulness
//
//  Created by Anders J.R. Petersen on 04/09/13.
//  Copyright (c) 2013 Anders J.R. Petersen. All rights reserved.
//

#import "MindfulClockViewController.h"
#import "CreateLocalNotificationsClocks.h"


@interface MindfulClockViewController ()
@end

static NSString *notificationsIsOnKey   =   @"notificationsIsOn";
static NSString *intervalMinutesKey     =   @"intervalMinutes";

@implementation MindfulClockViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //init switch
    [self.switchMindfulClock setOn:[[NSUserDefaults standardUserDefaults] boolForKey:notificationsIsOnKey]];
    
    //init sliderbar
    [self.sliderBar setEnabled:![[NSUserDefaults standardUserDefaults] boolForKey:notificationsIsOnKey]];
    [self setSliderMinutValue:[[NSUserDefaults standardUserDefaults] integerForKey:intervalMinutesKey]];
    
    //init label
    [self setupIntervalLabel];
    [self.intervalLabel setFont:[UIFont fontWithName:@"Didot-Bold" size:15.0]];
    self.intervalLabel.textAlignment = NSTextAlignmentRight;

}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

-(void)setupIntervalLabel{
    self.intervalLabel.text = [[NSString alloc] initWithFormat:@"%d minutter",[self getSliderMinuteValue]];
}

-(int)getSliderMinuteValue{
    return (int)(self.sliderBar.value*108) + 12;
}

-(void)setSliderMinutValue:(int) minuttes{
    self.sliderBar.value = (minuttes-12.0f)/108.0f;
}

- (IBAction)sliderChanged:(id)sender {
    [self setupIntervalLabel];
}

- (IBAction)switchChange:(id)sender {
    if(self.switchMindfulClock.isOn){
        [self.sliderBar setEnabled:NO];
        [CreateLocalNotificationsClocks createNotifications:[self getSliderMinuteValue]];
       // [self printNotifications];

    }else{
        [self.sliderBar setEnabled:YES];
        [CreateLocalNotificationsClocks deleteNotifications];
      //  [self printNotifications];
    }
}

-(void) printNotifications{
    NSLog(@"Local notifications from system:");

    
    UIApplication* objApp = [UIApplication sharedApplication];
    NSArray*    oldNotifications = [objApp scheduledLocalNotifications];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd. MMMM HH:mm:ss"];
    
        NSLog(@"Iall notification count is: %d", oldNotifications.count);
    
    for(UILocalNotification *notif in oldNotifications){
        NSString *strDate = [dateFormatter stringFromDate:[notif fireDate]];
        NSLog(@"%@", strDate);
    }
}

@end
